import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:yysk_app/utils/helper.dart';
import 'package:lottie/lottie.dart';

class LazyImage extends StatelessWidget {
  final String imageUrl;
  final double width;
  final double height;
  final double borderRadius;
  final BoxFit boxFit;
  final bool isShowLoading;
  final bool aotuHeight;
  final bool aotuWidth;
  final double borderRadiusTopLeft;
  final double borderRadiusTopRight;
  final double borderRadiusBottomLeft;
  final double borderRadiusBottomRight;
  final Alignment alignment;
  const LazyImage({
    super.key,
    required this.imageUrl,
    this.width = 12,
    this.height = 12,
    this.borderRadius = 8,
    this.aotuWidth = false,
    this.boxFit = BoxFit.fill,
    this.isShowLoading = true,
    this.aotuHeight = false,
    this.borderRadiusTopLeft = 0,
    this.borderRadiusTopRight = 0,
    this.borderRadiusBottomLeft = 0,
    this.borderRadiusBottomRight = 0,
    this.alignment = Alignment.center,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        borderRadius: BorderRadius.only(
          topLeft:
              borderRadiusTopLeft != 0
                  ? Radius.circular(borderRadiusTopLeft.px)
                  : Radius.circular(borderRadius.px),
          topRight:
              borderRadiusTopRight != 0
                  ? Radius.circular(borderRadiusTopRight.px)
                  : Radius.circular(borderRadius.px),
          bottomLeft:
              borderRadiusBottomLeft != 0
                  ? Radius.circular(borderRadiusBottomLeft.px)
                  : Radius.circular(borderRadius.px),
          bottomRight:
              borderRadiusBottomRight != 0
                  ? Radius.circular(borderRadiusBottomRight.px)
                  : Radius.circular(borderRadius.px),
        ),
      ),
      clipBehavior: Clip.antiAlias,
      child: CachedNetworkImage(
        alignment: alignment,
        fit: boxFit,
        width: aotuWidth ? null : width,
        height: aotuHeight ? null : height,
        imageUrl: imageUrl,
        placeholder:
            (context, url) =>
                isShowLoading
                    ? Center(
                      child: Lottie.asset(
                        'static/json/kongfu.json',
                        width: 40.px,
                        height: 40.px,
                      ),
                    )
                    : const SizedBox(),
        errorWidget: (context, url, error) => const Icon(Icons.error),
      ),
    );
  }
}
